//定义对象存储bom参数

var keyMap = {};
var pageid  = location.search.substring(1);
pageid.split('&').forEach(function(element){
    var km = element.split('=');
    keyMap[km[0]]= km[1];
},this);
var num = 0;
var Item ;
$(function(){
    Item = $('.product_item').clone(true);
    $('.product_item').remove();
    show(num);
    
    $('.nextpage').on('click',function(){
    var pag = $('.select_page').attr('pagesize');
        // console.log(pag);
        num++;
        if(num > pag-1){
            num = pag-1;
        }
        show(num);
    })
    $('.lastpage').on('click',function(){
        num--;
        if(num<0){
            num = 0;
        }
        show(num);
    })
    $('.select_page').on('change',function(){
        num = $(this).val()-0-1;
        // console.log(typeof num);
        show(num);
    })
})

function show(page){
    // console.log(page);
    $.get('http://127.0.0.1:3000/api/getmoneyctrl',{pageid:page},function(data){
         var maxPage = Math.ceil(data.totalCount/data.pagesize);
        // console.log(maxPage);
        data = data.result;
        var len = data.length;
        //生成列表
        $('.discount').empty();
        for(var i = 0; i < len ; i++){
            var idata = data[i];
            // console.log(idata);
            var commit = fornum(idata.productComCount);
            // console.log(commit);
            var Itemlist = Item.clone(true);
            Itemlist.find('.price');
            Itemlist.find('.product_img').html(idata.productImgSm);
            Itemlist.find('.pTitle').text(idata.productName);
             Itemlist.find('.pTitle').attr('href','inlandproduct.html?productid='+idata.productId);
            Itemlist.find('.price').text(idata.productPinkage);
            // console.log(idata.productPinkage);
            Itemlist.find('.productFrom').text(idata.productFrom);
            Itemlist.find('.productTime').text(idata.productTime);
            Itemlist.find('.commit').text(commit);
            $('.discount').append(Itemlist);
        }
        // 生成下拉框,置空
        $('.select_page').attr('pagesize',maxPage);
        $('.select_page').empty();
        for(var k = 1; k<maxPage+1; k++){
            $(".select_page").append($("<option/>", {
                    value: k,
                    text: k
            }));
        }
        $(".select_page option:first-child").attr('selected','selected');
        $('.select_page').val(num+1);
        $(".maxPage").text((num+1));
    })
}
function fornum(arg){
    var value = arg.replace(/[^0-9]/ig,"");
    return value;
}